# -*- coding: utf-8 -*-
# @Date    : 2017-04-26 16:41:02
# @Author  : fancy (fancy@thecover.co)
import time
import string
import random
import os.path
import hashlib
from captcha.image import ImageCaptcha

from conf import BASE_DIR


Elements = '%s%s' % (string.digits, string.ascii_letters)
Ttf_path = os.path.join(BASE_DIR, 'app/utils/XeroxSansSerifWideBold.ttf')
Captcha = ImageCaptcha(
    width=320, height=80, fonts=[Ttf_path])

def get_now():
    return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())


def generate_captcha():
    code = ''.join(random.sample(Elements, 5))
    im = Captcha.generate_image(code)
    path = '/static/tmp/%s.png' % hashlib.md5(
                        im.tobytes()).hexdigest()[:15]
    im.save('%s/app%s' % (BASE_DIR, path))
    return (code.lower(), path)
